package db;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MySQLFormatDate extends Date {

    private static final long serialVersionUID = 1L;

    public static String toString(Date d) {
        if (d == null) {
            return "0000-00-00";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(d);
    }

    public static String toString(Calendar cal) {
        if (cal == null) {
            return "0000-00-00";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    public static Calendar toCalendar(String str) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date date = df.parse(str);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            return cal;
        } catch (ParseException ex) {
            Logger.getLogger(MySQLFormatDate.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public static Date toDate(String str) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy");
            Date date = df.parse(str);
            return date;
        } catch (ParseException ex) {
            Logger.getLogger(MySQLFormatDate.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static String getYearString(Date d) {
        if (d == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return sdf.format(d);
    }
}
